package com.fancy.learncenter.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.activity.base.BaseActivity;
import com.fancy.learncenter.adapter.base.CommonRecycleViewAdapter;
import com.fancy.learncenter.adapter.base.CustomViewHold;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.bean.TrainApplyDetailDataBean;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.utils.ToastUtil;
import com.fancy.learncenter.utils.WXShareUtil;
import com.fancy.learncenter.view.PopupWindowShare;
import com.superservice.lya.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TrainApplyDetailActivity extends BaseActivity {

    String trainId = "";
    @Bind(R.id.applyRecycleView)
    RecyclerView applyRecycleView;
    @Bind(R.id.webView)
    WebView webView;
    @Bind(R.id.local)
    TextView local;
    @Bind(R.id.time)
    TextView time;
    @Bind(R.id.contact)
    TextView contact;
    @Bind(R.id.price)
    TextView price;
    @Bind(R.id.apply_num)
    TextView applyNum;
    @Bind(R.id.apply)
    Button apply;
    @Bind(R.id.simpleDraweeView)
    SimpleDraweeView simpleDraweeView;
    @Bind(R.id.rootView)
    LinearLayout rootView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_train_apply_detail, getResources().getColor(R.color.toolbar_color));
        ButterKnife.bind(this);
        initToolbar("培训报名");

        trainId = getIntent().getStringExtra("trainId");

        webView.getSettings().setJavaScriptEnabled(true);

        setRightImg(R.mipmap.ic_fenxiang, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popupWindowShare == null) {
                    popupWindowShare = new PopupWindowShare(TrainApplyDetailActivity.this, rootView);
                    popupWindowShare.setOnClickCallBack(new PopupWindowShare.OnClickCallBack() {
                        @Override
                        public void wxFriendCallBack() {
                            WXShareUtil.getInstance(TrainApplyDetailActivity.this).shareWebPage(shareUrlPic, shareTitle, shareContent, shareLinkUrl, true);
                        }

                        @Override
                        public void wxCallBack() {
                            WXShareUtil.getInstance(TrainApplyDetailActivity.this).shareWebPage(shareUrlPic, shareTitle, shareContent, shareLinkUrl, false);
                        }
                    });
                }
                popupWindowShare.showAtLocation();

            }
        });

        initInviteView();


    }

    private void initInviteView() {
        ArrayList videoDatas = new ArrayList<>();

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        applyRecycleView.setLayoutManager(linearLayoutManager);

        adapter = new CommonRecycleViewAdapter<TrainApplyDetailDataBean.EnrollListBean>(this, R.layout.fragment_trainapplay_person_item, videoDatas) {
            @Override
            public void bindView(CustomViewHold customViewHold, TrainApplyDetailDataBean.EnrollListBean item, int position) {
                SimpleDraweeView simpleDraweeView = customViewHold.getView(R.id.simpleDraweeView);

                simpleDraweeView.setImageURI(item.getEnroll_user_img());
            }
        };
        applyRecycleView.setAdapter(adapter);

    }


    private CommonRecycleViewAdapter adapter;

    @Override
    protected void onResume() {
        super.onResume();
        getDatas();
    }

    PopupWindowShare popupWindowShare;

    String shareUrlPic = "";
    String shareLinkUrl = "";
    String shareTitle = "";
    String shareContent = "";

    private void getDatas() {
        Map map = new HashMap();
        map.put("trainId", trainId);
        HttpMehtod.getInstance().trainApplyDetail(map, new IdeaObserver<BaseDataBean<TrainApplyDetailDataBean>>(this, true) {

            @Override
            public void onSuccess(BaseDataBean<TrainApplyDetailDataBean> trainApplyDetailDataBeanBaseDataBean) {

                if (trainApplyDetailDataBeanBaseDataBean.getData() == null) {
                    return;
                }


                shareUrlPic = trainApplyDetailDataBeanBaseDataBean.getData().getShare_img();
                shareContent = trainApplyDetailDataBeanBaseDataBean.getData().getShare_content();
                shareLinkUrl = trainApplyDetailDataBeanBaseDataBean.getData().getShare_link();
                shareTitle = trainApplyDetailDataBeanBaseDataBean.getData().getShare_title();

                simpleDraweeView.setImageURI(trainApplyDetailDataBeanBaseDataBean.getData().getPoster());

                LogUtil.MyLog("getHtmlData", "========getHtmlData=" + trainApplyDetailDataBeanBaseDataBean.getData().getContent());
                webView.loadDataWithBaseURL(null, getHtmlData(trainApplyDetailDataBeanBaseDataBean.getData().getContent()), "text/html", "utf-8", null);

                contact.setText(trainApplyDetailDataBeanBaseDataBean.getData().getContact1());

                local.setText(trainApplyDetailDataBeanBaseDataBean.getData().getPlace());

                time.setText(trainApplyDetailDataBeanBaseDataBean.getData().getBegin_time() + " - " + trainApplyDetailDataBeanBaseDataBean.getData().getEnd_time());

                price.setText("￥" + trainApplyDetailDataBeanBaseDataBean.getData().getFee());

                if (trainApplyDetailDataBeanBaseDataBean.getData().getEnrollList() != null) {
                    applyNum.setText(trainApplyDetailDataBeanBaseDataBean.getData().getEnrollList().size() + "人");
                    adapter.notifyDataSetChanged((ArrayList) trainApplyDetailDataBeanBaseDataBean.getData().getEnrollList());
                }

                if (trainApplyDetailDataBeanBaseDataBean.getData().getIs_enroll() == 0) {
                    apply.setText("立即报名");
                } else {
                    apply.setText("已报名");
                    apply.setBackgroundColor(Color.parseColor("#CCCCCC"));
                    apply.setClickable(false);

                }
                if (trainApplyDetailDataBeanBaseDataBean.getData().getIs_collect() == 0) {
                    mCollect = false;
//                    shoucangIcon.setImageResource(R.mipmap.ic_shoucang_gray);


                    setRight("收藏", rightOnclick);

                } else {
                    mCollect = true;
//                    shoucangIcon.setImageResource(R.mipmap.ic_shoucangdown);

                    setRight("已收藏", rightOnclick);
                }


            }

            @Override
            public void onFail(int errorCode) {

            }
        });
    }

    View.OnClickListener rightOnclick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mCollect) {
                cancelCollect();
            } else {
                collect();
            }

        }
    };


    private void collect() {
        Map map = new HashMap();
        map.put("relationId", trainId + "");
        map.put("type", "3");

        HttpMehtod.getInstance().collect(map, new IdeaObserver<BaseDataBean>() {


            @Override
            public void onSuccess(BaseDataBean baseDataBean) {
                mCollect = true;

                setRight("已收藏", rightOnclick);

                ToastUtil.show("收藏成功");
            }

            @Override
            public void onFail(int errorCode) {

            }
        });

    }

    private void cancelCollect() {
        Map map = new HashMap();
        map.put("relationId", trainId + "");
        map.put("type", "3");

        HttpMehtod.getInstance().cancelCollect(map, new IdeaObserver<BaseDataBean>() {


            @Override
            public void onSuccess(BaseDataBean baseDataBean) {
                mCollect = false;

                setRight("收藏", rightOnclick);

                ToastUtil.show("取消收藏");
            }

            @Override
            public void onFail(int errorCode) {

            }
        });

    }


    boolean mCollect;

    private String getHtmlData(String bodyHTML) {
        String head = "<head>" +
                "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> " +
                "<style>img{max-width: 100%; width:auto; height:auto;}</style>" +
                "</head>";
        return "<html>" + head + "<body>" + bodyHTML + "</body></html>";
    }

    @OnClick(R.id.apply)
    public void onViewClicked() {
        Intent intent = new Intent(this, TrainApplyActivity.class);
        intent.putExtra("trainId", trainId);
        startActivity(intent);
    }
}
